package socratesteam.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import socratesteam.model.dominio.Tipo;
import socratesteam.model.excepcion.DAOExcepcion;
import socratesteam.model.util.ConexionBD;

public class TipoDAO extends BaseDAO{

	private static final long serialVersionUID = 5975684368262255336L;

	private Connection oConnection = null;
	private PreparedStatement oStatement = null;
	private ResultSet oSet = null;
	private String query = "";
	
	public List<Tipo> listarTipos(Tipo oTipo) throws DAOExcepcion {
		List<Tipo> lista = new ArrayList<Tipo>();
		Tipo o;
		try {
			query = "SELECT T.N_CODIGO, T.C_NOMBRE_CORTO, T.C_NOMBRE_LARGO " +
					"FROM TAB_TIPO T " +
					"WHERE T.N_CODIGO_TABLA = " + oTipo.getCodigoTabla() + " " +
					"AND T.N_ESTADO = 1";
			oConnection = ConexionBD.obtenerConexion();
			oStatement = oConnection.prepareStatement(query);
			oSet = oStatement.executeQuery();
			while (oSet.next()) {
				o = new Tipo();
				o.setCodigo(oSet.getInt(1));
				o.setNombreCorto(oSet.getString(2));
				o.setNombreLargo(oSet.getString(3));
				lista.add(o);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(oSet);
			this.cerrarStatement(oStatement);
			this.cerrarConexion(oConnection);
		}
		return lista;
	}
}
